﻿using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyThongTinNhanKhau.GUI
{
    public partial class TamVang : Form
    {
        private GUI.NhanKhau nhanKhau;
        static String maNhanKhau;
        model.BO.TamVangBO tamVangBO;

        public TamVang(GUI.NhanKhau f, String maNK)
        {
            InitializeComponent();

            this.nhanKhau = f;
            maNhanKhau = maNK;
        }

        private void TamVang_Load(object sender, EventArgs e)
        {
            tamVangBO = new model.BO.TamVangBO(this, nhanKhau);

            this.tbMaNhanKhau.Text = maNhanKhau;

            tamVangBO.loadDefault(maNhanKhau);
            tamVangBO.loadTable(maNhanKhau);

            //Visible Panel Top.
            panelTop.Visible = false;
            this.gridControlTamVang.Location = new System.Drawing.Point(1, 2);
            this.gridControlTamVang.Size = new System.Drawing.Size(989, 629);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            panelTop.Visible = true;
            this.gridControlTamVang.Location = new System.Drawing.Point(1, 259);
            this.gridControlTamVang.Size = new System.Drawing.Size(989, 372);

            tamVangBO.genarateMaTamVang();

            cboTinhTrang.SelectedIndex = 0;
        }

        private void gridViewTamVang_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hitInfo = view.CalcHitInfo(e.Point);

            if (hitInfo.InRow)
            {
                view.FocusedRowHandle = hitInfo.RowHandle;
                this.contextMenuStripTamVang.Show(view.GridControl, e.Point);
            }
            else
            {
                this.contextMenuStripTamVang.Show(view.GridControl, e.Point);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            //Visible Panel Top.
            panelTop.Visible = false;
            this.gridControlTamVang.Location = new System.Drawing.Point(1, 2);
            this.gridControlTamVang.Size = new System.Drawing.Size(989, 629);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            tamVangBO.btnLuu();
        }

        private void sửaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewTamVang.SelectedRowsCount > 0)
            {
                panelTop.Visible = true;
                this.gridControlTamVang.Location = new System.Drawing.Point(1, 259);
                this.gridControlTamVang.Size = new System.Drawing.Size(989, 372);

                tbMaTamVang.Text = gridViewTamVang.GetRowCellValue(gridViewTamVang.FocusedRowHandle, "MaTamVang").ToString();
                cboTinhTrang.SelectedItem = gridViewTamVang.GetRowCellValue(gridViewTamVang.FocusedRowHandle, "TinhTrang").ToString();
                dateTimeNgayTamVang.Text = gridViewTamVang.GetRowCellValue(gridViewTamVang.FocusedRowHandle, "NgayTamVang").ToString();
                tbLyDoTamVang.Text = gridViewTamVang.GetRowCellValue(gridViewTamVang.FocusedRowHandle, "LyDoTamVang").ToString();
            }
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewTamVang.SelectedRowsCount > 0)
            {
                String maTamVang = gridViewTamVang.GetRowCellValue(gridViewTamVang.FocusedRowHandle, "MaTamVang").ToString();
                String maNhanKhau = gridViewTamVang.GetRowCellValue(gridViewTamVang.FocusedRowHandle, "MaNhanKhau").ToString();

                DialogResult result = MessageBox.Show("Bạn có muốn xóa Mã Tạm Vắng: " + maTamVang + "???", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                //If Yes
                if (result == DialogResult.Yes)
                {
                    tamVangBO.deleteRow(maTamVang , maNhanKhau);
                }
            }
        }
    }
}
